package com.hevs.samplewebapp.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TabPanel;
import com.hevs.samplewebapp.client.screenComponents.ScreenComponentAdminInformation;
import com.hevs.samplewebapp.client.screens.ManageUsersWidget;

/**
 * This is the administrative panel<br/>
 * <p>The Goal is to provide different tools to the administrator of the system.
 * The class is designed to host different tools with the possibility of adding new one easily. 
 * The Administrator will be able to manage users with this interface.</p>  
 * @author Thomas Rouvinez
 * @Date: 26.03.2012
 */
public class Administration extends Composite{

	// ------------------------------------------------------------
	// VARIABLES
	// ------------------------------------------------------------

	//Graphical Elements
	/**
	 * Administration panel
	 * <p>Panel with multiple tabs, to make easy the adding of new features for administration.</p>
	 */
	private TabPanel tabPanel = new TabPanel();

	/**
	 * Managing user tool
	 * <p>Widget that content the tools for adding, deleting and updating a user.</p>
	 * @see ManageUsersWidget
	 */
	public static ManageUsersWidget manageUsersWidget;

	/**
	 * Information panel
	 * <p>Widget that content main information to the administrator</p>
	 * @see ScreenComponentAdminInformation
	 */
	private ScreenComponentAdminInformation information;

	// URL
	/**
	 * Web Service URL
	 * <p>URL for the administration of users web service. It supports POST, PUT and GET</p>
	 */
	public static final String JSON_URL_ADMIN = GWT.getModuleBaseURL() + "ws/ressources/users";

	/**
	 * Web Service URL
	 * <p>URL for the administration of users web service, just for deleting. It supports POST</p>
	 */
	public static final String JSON_URL_ADMIN_DEL = GWT.getModuleBaseURL() + "ws/delete/users";

	/**
	 * Web Service URL
	 * <p>URL for the administration of users to reset the password. It supports only POST</p>
	 */
	public static final String JSON_URL_RESET_PASSWORD = GWT.getModuleBaseURL() + "ws/security/passreset";

	//------------------------------------------------------------
	// CONSTRUCTORS
	//------------------------------------------------------------

	/**
	 * Constructor of the administrative tools
	 * <p>It has a tab within the widget to manage the users. 
	 *  The tab has a listener to refresh and display again the list of users when we click on it. 
	 *  Automatically the first tab will be opened on the initialization of the object.</p>
	 *  @see ManageUsersWidget#RefreshCurrentCellTable()
	 */
	public Administration(){

		// Create both administration tabs
		manageUsersWidget = new ManageUsersWidget();
		information = new ScreenComponentAdminInformation();

		// Tab within the widget for managing the users
		tabPanel.add(manageUsersWidget, "Users Management");
		tabPanel.add(information, "Information");

		// show the information tab at the beginning
		tabPanel.selectTab(1);

		// Listener to refresh the content of the table on click
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
			// Refresh the tab every time we click on it
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				manageUsersWidget.RefreshCurrentCellTable();
			}
		});

		//initialize the widget
		initWidget(tabPanel);
	}
}